# 19 - Descriptive tables
rm(list=ls())
gc()
library(data.table)
library(modelsummary)
library(stringr)
library(dplyr)
library(xtable)
library(tibble)
library(tidyr)
library(gt)
library(readr)


data = fread('linked-unlinked-2012-2016.csv.gz',select=c('DemBlockGroup_year2', 'DemBlockGroup2_year2','RepBlockGroup','RepBlockGroup_year2','RepBlockGroup2_year2','Party_year2','Move'))[Move==1]

data[!Party_year2%in%c('Democrat','Republican'),Party_year2:='Non-Partisan']
t1=data[,list(

          DemBlockGroup_year2_old = mean(DemBlockGroup_year2,na.rm=T),
          DemBlockGroup_year2_new = mean(DemBlockGroup2_year2,na.rm=T),
          
 
          RepBlockGroup_year2_old = mean(RepBlockGroup_year2,na.rm=T),
          RepBlockGroup_year2_new = mean(RepBlockGroup2_year2,na.rm=T)
          
          
          
), by = 'Party_year2']

data = fread('linked-unlinked-2016-2020.csv.gz',select=c('DemBlockGroup_year2', 'DemBlockGroup2_year2','RepBlockGroup','RepBlockGroup_year2','RepBlockGroup2_year2','Party_year2','Move'))[Move==1]

data[!Party_year2%in%c('Democrat','Republican'),Party_year2:='Non-Partisan']

t2 = data[,list(
  
  DemBlockGroup_year2_old = mean(DemBlockGroup_year2,na.rm=T),
  DemBlockGroup_year2_new = mean(DemBlockGroup2_year2,na.rm=T),
  
  
  RepBlockGroup_year2_old = mean(RepBlockGroup_year2,na.rm=T),
  RepBlockGroup_year2_new = mean(RepBlockGroup2_year2,na.rm=T)
  
  
  
), by = 'Party_year2']


# 
t1[,Party:=Party_year2]
t1[,Party_year2:=NULL]
t2[,Party:=Party_year2]
t2[,Party_year2:=NULL]

t = merge(t1,t2,by='Party')

out = t %>%
  as_tibble %>%
  gt()%>%
  fmt_number(columns = 2:9, decimals = 3)%>%
  cols_label(
    `DemBlockGroup_year2_old.x` = 'Origin',
    `DemBlockGroup_year2_new.x` = 'New',
    `DemBlockGroup_year2_old.y` = 'Origin',
    `DemBlockGroup_year2_new.y` = 'New',
    
    `RepBlockGroup_year2_old.x` = 'Origin',
    `RepBlockGroup_year2_new.x` = 'New',
    `RepBlockGroup_year2_old.y` = 'Origin',
    `RepBlockGroup_year2_new.y` = 'New'
  )%>%
  tab_spanner(label = 'Prop. Democrat', 2:3)%>%
  tab_spanner(label = 'Prop. Republican', 4:5)%>%
  tab_spanner(label = 'Prop. DemocratREMOVE', 6:7)%>%
  tab_spanner(label = 'Prop. RepublicanREMOVE', 8:9)%>%
  tab_spanner(label = '2012-2016',columns = 2:5)%>%
  tab_spanner(label = '2016-2020',columns = 6:9)%>%

  
  
  as_latex()%>%
  as.character()%>%
  str_replace_all('longtable','tabular')%>%
  str_replace_all('REMOVE','')

write_file(out, 'tables/TabS4.tex')


